<?php
/**
 * Copyright (C) "Code Generator Tools"
 * @author liya(358103599@qq.com)
 */

require_once 'DB.php';
require_once 'GenHelp.php';

/**
 * CI控制器生成类
 */
class CIControllerGen
{
	private $tables;
	private $savePath;
	private $iOutPut;
	public function __construct($tables, $saveBase, $iOutPut)
	{
		$this->tables = $tables;
		$this->savePath = $saveBase.'controllers/admin/';
		$this->iOutPut = $iOutPut;

		if(!file_exists($this->savePath))
		{
			mkdir($this->savePath, 0777, true);
		}
	}

	public function Gen()
	{
		if(empty($this->tables))
		{
			$this->iOutPut->WriteLine('No Tables');
			return;
		}

		$this->iOutPut->WriteHeader('Controllers('.count($this->tables).')');

		foreach($this->tables as $table)
		{
			$tablename = $table['TABLE_NAME'];
			$filename = $this->savePath.strtolower($tablename).'.php';
			$result = $this->GenController($tablename, $filename);
			
			$gen_state_str = ($result)?'OK':'Fail';
			$file_save_datetime = GenHelp::ZhDate();
			$this->iOutPut->WriteLine("generator ".$gen_state_str." $filename (".filesize($filename)." Byte) -- $file_save_datetime");
		}
	}

	private function GenController($tablename, $filename)
	{
		$db = new DB();

		$classname = ucfirst(strtolower($tablename));		
		$columns = $db->GetTableColumns($tablename);

		//读取模板文件到字符串变量 $model_str 中
		$model_template_file = dirname(__FILE__).'/codeigniter/_Template/controller.php';
		$model_str = file_get_contents($model_template_file);

		/*
		 * 替换模板文件中的标记
		 * 
		 * {_datetime_}
		 * {_classname_}
		 * {_tablename_}
		 * {_load_model_name_}
		 * {_index_view_file_}
		 * {_controller_name_}
		 * {_detail_view_file_}
		 * {_primary_key_params_}
		 * {empty_check(_primary_key_params_)}
		 *
		 */
		$model_new_str = $model_str;
		$model_new_str = str_replace('{_datetime_}', GenHelp::ZhDate(), $model_new_str);
		$model_new_str = str_replace('{_classname_}', ucfirst($tablename), $model_new_str);
		$model_new_str = str_replace('{_tablename_}', $tablename, $model_new_str);
		$model_new_str = str_replace('{_load_model_name_}', ucfirst($tablename).'_model', $model_new_str);
		$model_new_str = str_replace('{_index_view_file_}', "admin/$tablename/list", $model_new_str);
		$model_new_str = str_replace('{_controller_name_}', strtolower($tablename), $model_new_str);
		$model_new_str = str_replace('{_detail_view_file_}', "admin/$tablename/detail", $model_new_str);

		// 替换 {_primary_key_params_}
		$primary_key_params_str = GenHelp::CreateFunctionParamsByPrimaryKey(GenHelp::GetPrimaryKeyColumns($columns));
		$model_new_str = str_replace('{_primary_key_params_}', $primary_key_params_str, $model_new_str);

		// 替换 {empty_check(_primary_key_params_)}
		$empty_check_primary_key_params = GenHelp::CreateEmptyCheckFunctionParamsByPrimaryKey(GenHelp::GetPrimaryKeyColumns($columns));
		$model_new_str = str_replace('{empty_check(_primary_key_params_)}', $empty_check_primary_key_params, $model_new_str);

		//保存替换后的字符串到文件 $filename
		return file_put_contents($filename, $model_new_str);
	}

}

?>